﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using AviationSimulator.DataItems;
using AviationSimulator.Properties;


namespace AviationSimulator.Managers
{
    public class PackageManager
    {
        private static int _packageCounter;

        public List<Package> TemporaryPackageStorage;

        public PackageManager()
        {
            TemporaryPackageStorage = new List<Package>();
        }

        public void SavePackageToFile(Package package, string filePath)
        {
            string pakageString = string.Empty;

            for (int index = 0; index < package.Parameters.Count(); index++)
            {
                pakageString += package.Parameters.ElementAt(index).Name + "=";
                pakageString += package.Parameters.ElementAt(index).Value;

                if (index < (package.Parameters.Count() - 1))
                    pakageString += ";";
            }

            if (File.Exists(filePath))
            {
                using (var streamWirter = File.AppendText(filePath))
                {
                    streamWirter.WriteLine(pakageString);
                }
            }
            else
            {
                using (var streamWriter = new StreamWriter(filePath))
                {
                    streamWriter.WriteLine(pakageString);
                }
            }
        }

        private static Package GetPackageFromString(string packageString)
        {
            var parameters = packageString.Split(';');

            var package = new Package();

            foreach (var parameter in parameters)
            {
                var parameterParts = parameter.Split('=');
                var newParameter = new Parameter 
                                       {
                                           Name = parameterParts[0], 
                                           Value = parameterParts[1]
                                       };

                package.Parameters.Add(newParameter);
            }

            package.Number = _packageCounter;
            _packageCounter++;

            return package;
        }

        public void SavePackagesFromTemp(string filePath)
        {
            File.Copy(Settings.Default.TempPackagesFile, filePath);
            ClearTempPackagesStorage();
        }

        public List<Package> GetPackagesFromFile(string filePath)
        {
            var packageStrings = new List<string>();
            var packages = new List<Package>();

            try
            {
                using (var file = new StreamReader(filePath))
                {
                    string line;
                    while ((line = file.ReadLine()) != null)
                    {
                        packageStrings.Add(line);
                    }
                }

                packages.AddRange(packageStrings.Select(GetPackageFromString));
            }
            catch (Exception)
            {
            }

            return packages;
        }

        public void ClearTempPackagesStorage()
        {
            TemporaryPackageStorage.Clear();

            if (File.Exists(Settings.Default.TempPackagesFile))
            {
                File.WriteAllText(Settings.Default.TempPackagesFile, string.Empty);
            }
        }

        public void AddPackageToTempStorage(Package package)
        {
            TemporaryPackageStorage.Add(package);
            SavePackageToFile(package, Settings.Default.TempPackagesFile);
        }
    }
}
